﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EasyNet.Solr;
using EasyNet.Solr.Commons;

namespace SolrExample.Tests.Temp
{
    public class SolrObjectDeSerializer : IObjectDeserializer<SolrSearchResultRecord>
    {
        public string GetString(SolrDocument doc, string name)
        {
            if (!doc.ContainsKey(name))
            {
                return string.Empty;
            }
            var docObject = doc[name];
            if (docObject == null)
            {
                return string.Empty;
            }
            return docObject.ToString();

        }

        public IEnumerable<SolrSearchResultRecord> Deserialize(SolrDocumentList result) {
            if (result == null)
            {
                yield return null;
            }

            foreach (SolrDocument doc in result)
            {
                var record = new SolrSearchResultRecord();
                
                record.Id = Convert.ToInt64(GetString(doc, "id"));
                record.Name = GetString(doc, "name");
                
                yield return record;
            }
        }
    }
}
